<template>
  <div>
    <el-dialog title="加入课程" v-model="dialogFormVisible">
      <el-form :model="form" :rules="rule" ref="addClassForm">
        <el-form-item label="加课码" :label-width="formLabelWidth" prop="addCourseCode">
          <el-input v-model="form.addCourseCode" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <template v-slot:footer>
        <div class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="submitForm">确 定</el-button>
        </div>
    </template>
    </el-dialog>
  </div>
</template>

<script>
import {parseToken} from "@/utils/jwtUtil";
import {postRequest} from "@/utils/api";

export default {
  name: 'addClassComponent',
  data() {
    return {
      dialogFormVisible: false,
      form: {
        addCourseCode: '',
      },
      rule: {
        addCourseCode: [
          { required: true, message: '请输入课程加课码', trigger: 'blur' },
        ]
      },
      formLabelWidth: '120px'
    };
  },
  methods: {
    open() {
      this.dialogFormVisible = true;
    },
    cancel(){
      this.dialogFormVisible = false;
      this.form = {}
    },
    submitForm() {
      this.$refs.addClassForm.validate((valid) => {
        if (valid) {
          const courseForm = {
            courseCode: this.form.addCourseCode,
            type:parseToken(localStorage.getItem('Token')).claims.type,
            userId:parseToken(localStorage.getItem('Token')).claims.id
          }
          postRequest('/userCourse/addClass', courseForm).then(res => {
            console.log(res)
          })
          this.dialogFormVisible = false;
        } else {
          console.log('error submit!!');
          return false;
        }
      });
      this.form = {}
    },
  }
};
</script>



<style scoped>

</style>